"""
# 读取整个文件内容
with open("example.txt", "r", encoding="utf-8") as file:
    content = file.read()
    print(content)

    # 使用 readline() 逐行读取
with open("example.txt", "r", encoding="utf-8") as file:
    line = file.readline()
    while line:
        print("====", line.strip())
        line = file.readline()

# 使用 readlines() 读取所有行
with open("example.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()
    for line in lines:
        print(line.strip())

# 直接遍历文件对象（推荐方式）
with open("example.txt", "r", encoding="utf-8") as file:
    for line in file:
        print(line.strip())

# 读取前100个字符
with open("example.txt", "r", encoding="utf-8") as file:
    chunk = file.read(10)
    print(chunk)

# 分块读取大文件
with open("large_file.txt", "r", encoding="utf-8") as file:
    while True:
        chunk = file.read(1000)  # 每次读取1000个字符
        if not chunk:  # 读取结束
            break
        print(chunk)

# 覆盖写入模式
with open("output.txt", "w", encoding="utf-8") as file:
    file.write("Hello, World!\n")
    file.write("This is a new line.\n")

# 追加写入模式
with open("output.txt", "a", encoding="utf-8") as file:
    file.write("This line is appended.\n")
# 准备要写入的字符串列表
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]

# 使用 writelines() 批量写入
with open("output.txt", "w", encoding="utf-8") as file:
    file.writelines(lines)

# 或者使用循环写入
with open("output.txt", "w", encoding="utf-8") as file:
    for line in lines:
        file.write(line)

with open("example.txt", "r", encoding="utf-8") as file:
    print(f"初始位置: {file.tell()}")  # 0
    content = file.read(5)
    print(f"读取内容: {content}")  # 前5个字符
    print(f"读取后位置: {file.tell()}")  # 5

# 1. 创建并写入文件
with open("demo.txt", "w", encoding="utf-8") as file:
    file.write("这是第一行\n")
    file.write("这是第二行\n")
    file.write("这是第三行\n")

# 2. 读取并显示文件内容
print("文件内容:")
with open("demo.txt", "r", encoding="utf-8") as file:
    for line_num, line in enumerate(file, 1):
        print(f"第{line_num}行: {line.strip()}")

# 3. 追加内容到文件
with open("demo.txt", "a", encoding="utf-8") as file:
    file.write("这是追加的内容\n")

# 4. 显示追加后的内容
print("\n追加后的内容:")
with open("demo.txt", "r", encoding="utf-8") as file:
    print(file.read())
"""

import csv

# 写入 CSV 文件
with open("data.csv", "w", newline="", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerow(["姓名", "年龄", "城市"])  # 写入表头
    writer.writerow(["张三", "25", "北京"])  # 写入数据
    writer.writerow(["李四", "30", "上海"])

# 读取 CSV 文件
with open("data.csv", "r", newline="", encoding="utf-8") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

import json

# 准备数据
data = {"name": "张三", "age": 25, "cities": ["北京", "上海", "广州"]}

# 写入 JSON 文件
with open("data.json", "w", encoding="utf-8") as file:
    json.dump(data, file, ensure_ascii=False, indent=4)

# 读取 JSON 文件
with open("data.json", "r", encoding="utf-8") as file:
    loaded_data = json.load(file)
    print(type(loaded_data))  # <class 'dict'>

# try:
#     with open("nonexistent.txt", "r", encoding="utf-8") as file:
#         content = file.read()
#         print(content)
# except FileNotFoundError:
#     print("文件不存在！")
# except PermissionError:
#     print("权限不足，无法访问文件！")
# except IOError as e:
#     print(f"文件操作错误: {e}")
# except UnicodeDecodeError:
#     print("文件编码错误！")
# finally:
#     print("文件操作完成")

with open("data.json", "r", encoding="utf-8") as file:
    print(type(file.read()))  # <class 'str'>
